import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/util/ext_utils.dart';
import 'package:ls_work/viewmodel/select_employee_view_model.dart';

import '../res/colors.dart';

class SelectEmployeePage extends BaseStatefulMvvmWidget {

  Function(SelectFoodHuman human) onSelectListener;

  final dynamic officeId;

  SelectEmployeePage({required this.onSelectListener,this.officeId});

  @override
  State<StatefulWidget> createState() => _SelectEmployeeState();

}

class _SelectEmployeeState
    extends BaseMvvmListState<SelectEmployeeViewModel, SelectEmployeePage> {
  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      backgroundColor: Colours.bg_color,
      appBar: AppBar(title: Text("选择员工"),),
      body: Column(
        children: [
          CommonInput(
            isDense: true,
            border: InputBorder.none,
            padding: EdgeInsets.zero,
            hintText: '请输入姓名或者首字母搜索',
            textInputAction: TextInputAction.search,
            onSubmitted: (str) {
              viewModel.requestRefresh(showAni: true);
            },
            onChanged: (str) {
              viewModel.nameLike = str;
            },
          ).addToContainer(
              margin: EdgeInsets.symmetric(horizontal: 16, vertical: 10),
              padding: EdgeInsets.all(10),
              decoration: BoxDecoration(
                  color: Colors.white, borderRadius: 4.borderRadius)
          ),
          loadingContentWidget.setWeight(1)
        ],
      ),
    );
  }

  @override
  Widget createItemWidget(int index) {
    var item = viewModel.listItems[index];
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            CommonText(item.name, textSize: 17, fontWeight: FontWeight.bold),
            CommonText('员工', textColor: Colours.app_main)
          ],
        ),
        Container(height: 10,),
        CommonText('部门：${item.officeName ?? ''}')
      ],
    ).onTap(() {
      if(widget.onSelectListener!=null){
        var human = SelectFoodHuman(type: 1,
            name: item.name,
            mobile: item.mobile,
            headImage: item.headimageId.toStringOrNull(),
            sex: item.sex == 0 ? "男" : "女",id: item.humanId.toStringOrNull(),officeName: item.officeName);
        widget.onSelectListener(human);
      }
    }).addToContainer(
        decoration: BoxDecoration(
            color: Colors.white, borderRadius: 8.borderRadius),
        padding: EdgeInsets.all(10),
        margin: EdgeInsets.symmetric(horizontal: 16)
    );
  }

  @override
  Widget get separatorDivider => Container(height: 10,);

  @override
  SelectEmployeeViewModel createViewModel() {
    return SelectEmployeeViewModel(context,officeId: widget.officeId);
  }

  @override
  bool get canPullUp => true;

}

class SelectFoodHuman {
  String? name;
  int? type;
  String? headImage;
  int? age;
  String? sex;
  String? id;
  String? bedNo;
  String? mobile;

  String get typeName => type == 0 ? "长者" : type == 1 ? "住户" : "临时";
  String? officeName;

  SelectFoodHuman({this.name, this.type, this.headImage, this.age, this.sex,
    this.id, this.officeName,this.bedNo,this.mobile});


}